package org.awa.demo.opencv.gltf.handler;

import lombok.extern.slf4j.Slf4j;
import org.awa.demo.opencv.gltf.Constant;
import org.awa.demo.opencv.gltf.SoundUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//斜边
@Slf4j
@Component
public class WallHypoHandler implements IRectangleHandler{
    @Override
    public List<float[]> getRectanglePointList(List<float[]> pointList) {
        //根据墙面特性，因为是平面，所以XY的坐标系肯定是同时最大同时最小，不然就成曲面了，而Z轴代表高度。
        // 所以根据x的最大最小值可以直接拿到对应y的值。
        // 面的四个点分别是[minX,minY,minZ],[maxX,maxY,minZ],[minX,minY,maxZ],[maxX,maxY,maxZ]
        float normalX = pointList.get(0)[0];

        Integer maxPointIndex = null;
        Integer minPointIndex = null;
        float maxZ = Constant.MIN_FLOAT;
        float minZ = Constant.MAX_FLOAT;
        float maxX = Constant.MIN_FLOAT;
        float minX = Constant.MAX_FLOAT;
        for (int i = 0; i < pointList.size(); i++) {
            float[] point  = pointList.get(i);
            float x = point[0];
            float z = point[2];
            if(maxX < x){
                maxX = x;
                maxPointIndex = i;
            }
            if(minX > x){
                minX = x;
                minPointIndex = i;
            }
            if(maxZ < z){
                maxZ = z;
            }
            if(minZ > z){
                minZ = z;
            }
        }

        List<float[]> resultList = new ArrayList<>();
        float[] minPoint = pointList.get(minPointIndex);
        float[] maxPoint = pointList.get(maxPointIndex);
        resultList.add(new float[]{
                minPoint[0],minPoint[1],minZ
        });
        resultList.add(new float[]{
                maxPoint[0],maxPoint[1],minZ
        });
        resultList.add(new float[]{
                maxPoint[0],maxPoint[1],maxZ
        });
        resultList.add(new float[]{
                minPoint[0],minPoint[1],maxZ
        });

        return resultList;
    }

    //网格化并获取各网格中心点
    @Override
    public List<float[]> getGridObject(List<float[]> pointList, int widthSize, int heightSize) {
        float[] xArr = SoundUtil.getPointListByLine(pointList.get(0)[0],pointList.get(1)[0],widthSize);
        float[] yArr = SoundUtil.getPointListByLine(pointList.get(0)[1],pointList.get(1)[1],widthSize);
        float[] zArr = SoundUtil.getPointListByLine(pointList.get(1)[2],pointList.get(2)[2],heightSize);

        List<float[]> resultList = new ArrayList<>();

        for (int i = 0; i < yArr.length; i++) {
            float y = yArr[i];
            float x = xArr[i];
            for (int j = 0; j < zArr.length; j++) {
                float z = zArr[j];
                resultList.add(new float[]{
                        x,y,z
                });
            }
        }

        return resultList;
    }
}
